const app = getApp()
const util = require("../../utils/utils.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    searchBoxValue: "",
    items: [],
    banners: [{
      picUrl: "/assets/banners/banner1.jpg",
      url: "",
      bannerId: "0"
    }, {
      picUrl: "/assets/banners/banner2.jpg",
      url: "",
      bannerId: "1"
    }, {
      picUrl: "/assets/banners/banner3.jpg",
      url: "",
      bannerId: "2"
    }]
  },

  onTappingCard: function (event) {
    app.globalData.dealingId = event.currentTarget.dataset.id
    wx.navigateTo({
      url: '../secondhand-detail/detail'
    })
  },

  scroll(e) {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      userInfo: app.globalData.userInfo
    })
    wx.setNavigationBarTitle({
      title: "校园二手交易平台"
    });
    var that = this
    wx.request({
      url: 'http://localhost:8888/dealing/getAllDealing',
      method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          items: res.data.data,
          searchBoxValue: ""
        })
        for (var i = 0; i < that.data.items.length; i++) {
          var iPhoto = "items[" + i + "].photo"
          that.setData({
            [iPhoto]: util.strToArr(that.data.items[i].photo)[0],
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  viewAd: function() {
    wx.navigateTo({
      url: '../advertising/ad',
    })
  },

  onSearchBoxChange(e) {
    this.setData({
      searchBoxValue: e.detail,
    });
  },
  onSearchBoxSearch() {
    app.globalData.dealingSearch = this.data.searchBoxValue;
  if (this.data.searchBoxValue == '') {

  } else {
    wx.navigateTo({
      url: '../secondhand-search/search',
    })
  }
},
  viewCart() {
      wx.navigateTo({
        url: '../secondhand-cart/cart',
      })
  }
})